home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Programming / tek / examples / string.c < prev    next >
C/C++ Source or Header  |  2001-05-18  |  2KB  |  138 lines

  1.  
  2. /* 
  3. **    tek/examples/string.c
  4. **
  5. **    demonstrates dynamic array handling.
  6. */
  7.  
  8.  
  9. #include <tek/array.h>
  10. #include <stdio.h>
  11.  
  12.  
  13.  
  14. int main (void)
  15. {
  16.     TSTRPTR s1, s2;
  17.     TINT pos;
  18.  
  19.     
  20.     /* 
  21.     **    for fun and experimentation, we let the entire string
  22.     **    handling operate in a small, static block of memory.
  23.     **    (note that there is no need for you to create a MMU
  24.     **    for a string - you may as well pass TNULL for the "mmu"
  25.     **    argument.)
  26.     */
  27.  
  28.     TBYTE staticbuffer[200];
  29.     TMMU mmu;
  30.     TMEMHEAD memhead;
  31.     
  32.     TInitMemHead(&memhead, staticbuffer, sizeof(staticbuffer), TNULL);
  33.     if (TInitMMU(&mmu, &memhead, TMMUT_Static, TNULL))
  34.     {
  35.         /* 
  36.         **    create an empty dynamic string, i.e. a string
  37.         **    pointing to a zero byte.
  38.         */
  39.         
  40.         s1 = TCreateString(&mmu, 0);
  41.         
  42.     
  43.         /* 
  44.         **    create another dynamic string from an initial string
  45.         */
  46.         
  47.         s2 = TCreateStringStr(&mmu, "a teklib");
  48.         
  49.     
  50.         if (s1 && s2)
  51.         {
  52.             /* 
  53.             **    append some text to the first string.
  54.             */
  55.             
  56.             TStringCatStr(&s1, "this is ");
  57.             TStringCat(&s1, s2);
  58.             TStringCatStr(&s1, " dynamic string.");
  59.             
  60.             /*
  61.             **    note that you can modify the string without
  62.             **    checking for success on each individual manipulation.
  63.             **
  64.             **    when a manipulation fails, the entire string object
  65.             **    will immediately fall into an "invalid" state and
  66.             **    simply ignore further modifications. when in invalid
  67.             **    state, any attempt to modify a string will return
  68.             **    TFALSE.
  69.             */
  70.     
  71.     
  72.             /*
  73.             **    query the valid state before the string is passed
  74.             **    to a function in the world outside:
  75.             */
  76.         
  77.             if (TStringValid(s1))
  78.             {
  79.                 printf("%s\n", s1);
  80.                 
  81.                 /*
  82.                 **    result: "this is a teklib dynamic string."
  83.                 */
  84.             }
  85.             else
  86.             {
  87.                 printf("sorry, the string is not valid. it ran out of memory.\n");
  88.             }
  89.             
  90.         
  91.             pos = TStringFind(s1, s2);    /* find s2 in s1, return position */
  92.             if (pos >= 0)
  93.             {
  94.                 /*
  95.                 **    modify the length of the string
  96.                 */
  97.             
  98.                 TStringSetLen(&s1, pos);
  99.             }
  100.     
  101.             
  102.             /* 
  103.             **    append some more text.
  104.             */
  105.             
  106.             TStringCatStr(&s1, "cool because it has an inbuilt memory manger.");
  107.     
  108.         
  109.             if (TStringValid(s1))
  110.             {
  111.                 printf("%s\n", s1);
  112.  
  113.                 /*
  114.                 **    result: "this is cool because it has an inbuilt memory manager."
  115.                 */
  116.             }
  117.             else
  118.             {
  119.                 printf("sorry, the string is not valid. it ran out of memory.\n");
  120.             }
  121.         
  122.         
  123.             TDestroyString(s1);
  124.             TDestroyString(s2);
  125.         }
  126.     }
  127.     else
  128.     {
  129.         printf("*** failed to init MMU on top of static allocator\n");
  130.     }
  131.  
  132.     fflush(NULL);
  133.  
  134.     return 0;
  135. }
  136.  
  137.  
  138.